package vn.dreamteam.vuigiaitri;

import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * @author ĐvHiện : Quản lý menu
 * */
public class MenuMananger extends MainActivity {

	private MainActivity main;

	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;
	private String mTitle = "";

	public MenuMananger() {
		main = MainActivity.getInstance();
	}

	public void init() {
		mTitle = (String) getTitle();
		mDrawerLayout = (DrawerLayout) main.findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) main.findViewById(R.id.drawer_list);
		mDrawerToggle = new ActionBarDrawerToggle(main, mDrawerLayout,
				R.drawable.ic_drawer, R.string.drawer_open,
				R.string.drawer_close) {
			/** Called when drawer is closed */
			public void onDrawerClosed(View view) {
				main.getActionBar().setTitle(mTitle);
				main.invalidateOptionsMenu();

			}

			/** Called when a drawer is opened */
			public void onDrawerOpened(View drawerView) {
				main.getActionBar().setTitle("Vui vật vã");
				main.invalidateOptionsMenu();
			}

		};

		mDrawerLayout.setDrawerListener(mDrawerToggle);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				main.getBaseContext(), R.layout.drawer_list_item, main
						.getResources().getStringArray(R.array.arr_menu));

		mDrawerList.setAdapter(adapter);

		main.getActionBar().setDisplayHomeAsUpEnabled(true);

		mDrawerList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String[] rivers = main.getResources().getStringArray(
						R.array.arr_menu);
				mTitle = rivers[position];
				Bundle data = new Bundle();
				data.putInt("position", position);
				switch (position) {
				case 0:
					CameraGiauKin cameraGiauKin = new CameraGiauKin(main);
					main.addFrameContent(cameraGiauKin);
					break;
				case 1:
					TaiNanBatNgo taiNanBatNgo = new TaiNanBatNgo(main);
					main.addFrameContent(taiNanBatNgo);
					break;
				case 2:
					DongVatVuiNhon dongVatVuiNhon = new DongVatVuiNhon(main);
					main.addFrameContent(dongVatVuiNhon);
					break;
				case 3:
					NhacCheChonLoc nhacCheChonLoc = new NhacCheChonLoc(main);
					main.addFrameContent(nhacCheChonLoc);
					break;
				case 4:
					HaiMienBac HaiMienBac = new HaiMienBac(main);
					main.addFrameContent(HaiMienBac);
					break;
				case 5:
					HaiMienNam haiMienNam = new HaiMienNam(main);
					main.addFrameContent(haiMienNam);
					break;
				case 6:
					NgheTruyenOnline ngheTruyenOnline = new NgheTruyenOnline(
							main);
					main.addFrameContent(ngheTruyenOnline);
					break;
				case 7:
					DanhDauChuaXem danhDauChuaXem = new DanhDauChuaXem(main);
					main.addFrameContent(danhDauChuaXem);
					break;
				case 8:
					UngDungKhac ungDungKhac = new UngDungKhac(main);
					main.addFrameContent(ungDungKhac);
					break;
				}
				mDrawerLayout.closeDrawer(mDrawerList);

			}
		});
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}